package zpx.dao;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.compass.core.util.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import zpx.idao.IMaterialDao;
import zpx.model.Material;


@SuppressWarnings("restriction")
@Repository("materialDao")
public class MaterialDao extends HibernateDaoSupport implements IMaterialDao {
  @Resource
 public void setSuperSessionFactory(SessionFactory sessionFactory) {
     this.setSessionFactory(sessionFactory);
 }
 
  /**
   * 获得物料数量
   */
  @Override
  public int getMaterialCount(Map<String, String> searchMap) {
	  	int total = 0;
		Long datacount= (Long) this.getSessionFactory().getCurrentSession()
				.createQuery("select count(*) from Material")
				.uniqueResult();
		total=datacount.intValue();
		return total;
  }

 /**
  * 获得文件列表数据
  */
 @SuppressWarnings("unchecked")
 @Override
 public List<Material> getMaterialsList(Map<String, String> searchMap) {
     StringBuffer sql = new StringBuffer();
        sql.append("from Material");
        Query query = this.getSessionFactory().getCurrentSession().createQuery(sql.toString());
		List<Material> datas = query.list();
        return  datas;
 }
 /**
  * 根据主键获得文件列表信息
  */
 @Override
 public Material getMaterial(String id) {
     if(id==null ||"".equals(id)){
            return null;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("from Material as m where m.id= :id ");
        Query query = this.getSessionFactory().getCurrentSession().createQuery(sql.toString());
        query.setParameter("id", id); 
        return (Material) query.uniqueResult();
 }

 /**
  * 保存删除物料对象
  * 
  * @param material 物料对象
  * @return 物料对象
  */
 @Override
 public Material saveOrUpdateMaterial(Material material) {
     if(StringUtils.hasText(material.getId())){
    	 this.getSessionFactory().getCurrentSession().update(material);
     }else{
    	 this.getSessionFactory().getCurrentSession().save(material);
     }
     return material;
 }
 /**
  * 删除物料
  * 
  * @param id 物料主键
  * @return 
  */
 @Override
 public void deleteMaterial(String id) {
     // TODO Auto-generated method stub
     
 }



 
}
